package com.sky.mapper;

import com.sky.annotation.AutoDefaultAvatar;
import com.sky.annotation.AutoFill;
import com.sky.annotation.AutoFillCommon;
import com.sky.dto.ResetPasswordDTO;
import com.sky.dto.UserInfoUpdateDTO;
import com.sky.dto.UserRegisterDTO;
import com.sky.entity.User;
import com.sky.enumeration.OperationType;
import com.sky.service.UserService;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @author 贾晓晨
 * @version 1.0
 */
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM users WHERE username = #{username} OR email = #{username}")
    User getByUsername(String username);

    @AutoDefaultAvatar
    @AutoFillCommon(value = OperationType.INSERT)
    @Insert("insert into users(username, nickname ,password, email, avatar, create_time, update_time) " +
            "values(#{userRegisterDTO.username},#{userRegisterDTO.username},#{pwd},#{userRegisterDTO.email}," +
            "#{userRegisterDTO.avatar},#{userRegisterDTO.createTime},#{userRegisterDTO.updateTime})")
    void register(UserRegisterDTO userRegisterDTO,String pwd);

    @Select("select * from  users where username = #{username}")
    User isUsernameExist(String username);

    @Select("select * from  users where email = #{email}")
    User isEmailExists(String email);

    @Update("update users set password = #{password} where email = #{email}")
    void resetPassword(String email,String password);

    @Update("update users set nickname = #{nickname},avatar = #{avatar} where id = #{id}")
    void updateInfo(UserInfoUpdateDTO userInfoUpdateDTO);

    @Select("select avatar from users where permission = 1")
    String getAvatar();

}
